package com.spring.boot.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.sax.handler.RowHandler;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spring.boot.entity.ChinaCity;
import com.spring.boot.entity.InOutNum;
import com.spring.boot.entity.InOutNumCurve;
import com.spring.boot.service.impl.ChinaCityServiceImpl;
import com.spring.boot.service.impl.InOutNumCurveServiceImpl;
import com.spring.boot.service.impl.InOutNumServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class MyUtils {

    private final static Logger log = LoggerFactory.getLogger(MyUtils.class);

    public static String getInOutStr(boolean isIn) {
        if (isIn) {
            return "move_in";
        } else {
            return "move_out";
        }
    }

    public static String getToday() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String today = formatter.format(new Date());//todo 这里生成的时间很有可能只是服务器启动的时间。
        return today;
    }
    public static String getTodayTime() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String today = formatter.format(new Date());//todo 这里生成的时间很有可能只是服务器启动的时间。
        return today;
    }
    public static String getMarkDate(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String today = formatter.format(date);//todo 这里生成的时间只是服务器启动的时间。
        return today;
    }

    public static String today() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String today = formatter.format(new Date());//todo 这里生成的时间只是服务器启动的时间。
        return today;
    }

    public static String getBeforeDay(String originDay) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date originDate = DateUtil.parse(originDay);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(originDate);
        calendar.add(Calendar.DATE, -1);
        String result = formatter.format(calendar.getTime());
        return result;
    }

    public static String getBeforeDays(String originDay, int days) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date originDate = DateUtil.parse(originDay);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(originDate);
        calendar.add(Calendar.DATE, days);
        String result = formatter.format(calendar.getTime());
        return result;
    }

    public static String getYesterday() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        Date today = new Date();//todo 这里生成的时间只是服务器启动的时间。
        Calendar calendar = Calendar.getInstance();//new一个Calendar类,把Date放进去
        calendar.setTime(today);
        //此时的日期为今天的日期,要实现昨天,日期应该减一
        calendar.add(Calendar.DATE, -1);
        String result = formatter.format(calendar.getTime());
        return result;
    }

    public static String getTwoDaysBefore() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        Date today = new Date();//todo 这里生成的时间只是服务器启动的时间。
        Calendar calendar = Calendar.getInstance();//new一个Calendar类,把Date放进去
        calendar.setTime(today);
        //此时的日期为今天的日期,要实现前天,日期应该减二
        calendar.add(Calendar.DATE, -2);
        String result = formatter.format(calendar.getTime());
        return result;
    }

    public static List<String> getJsonKeys(JSONObject jsonObject) {
        List<String> keyList = new ArrayList<>();
        Set<Map.Entry<String, Object>> entries = jsonObject.entrySet();
        for (Map.Entry<String, Object> entry : entries) {
            String key = entry.getKey();
//            log.info("key:{}",key);
            keyList.add(key);
//            Object value = entry.getValue();
//            log.info("value:{}",value);
        }
        return keyList;
    }

    public static List<InOutNum> getMoveInCities(String targetCityName, String date){
        InOutNumServiceImpl inOutNumService = SpringContextUtils.getBean("inOutNumServiceImpl",InOutNumServiceImpl.class);
        QueryWrapper<InOutNum> queryWrapper = new QueryWrapper<>();
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("type", "in");
        paramMap.put("target_city", targetCityName);
        paramMap.put("date", StringUtils.replace(date,"-",""));
        queryWrapper.allEq(paramMap);
        List<InOutNum> list = inOutNumService.list(queryWrapper);
        if (list.size() == 0){
            list = getMoveInCities(targetCityName,getBeforeDays(date,-1));
        }
        return list;
    }

    public static InOutNumCurve getMoveInNum(String cityName, String date){
        InOutNumCurveServiceImpl inOutNumCurveService = SpringContextUtils.getBean("inOutNumCurveServiceImpl",InOutNumCurveServiceImpl.class);
        QueryWrapper<InOutNumCurve> curveQueryWrapper = new QueryWrapper<>();
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("city", cityName);
        paramMap.put("type", "in");
        paramMap.put("date", StringUtils.replace(date,"-",""));
        curveQueryWrapper.allEq(paramMap);
        InOutNumCurve curve = inOutNumCurveService.getOne(curveQueryWrapper);
        if (ObjectUtil.isNull(curve)){
            curve = getMoveInNum(cityName,getBeforeDays(date,-1));
        }
        return curve;
    }

    public static ChinaCity getCityInfo(String cityName, String date,int retryTimes){
        ChinaCityServiceImpl chinaCityService = SpringContextUtils.getBean("chinaCityServiceImpl",ChinaCityServiceImpl.class);
        QueryWrapper<ChinaCity> chinaCityQueryWrapper = new QueryWrapper<>();
        if (cityName.contains("张家")){
            chinaCityQueryWrapper.likeRight("city",cityName.substring(0,3)).likeRight("create_time",date);
        }else {
            chinaCityQueryWrapper.likeRight("city",cityName.substring(0,2)).likeRight("create_time",date);

        }

        ChinaCity chinaCity = chinaCityService.getOne(chinaCityQueryWrapper);//根据城市名和日期查询的，正常应该只有一条数据

        if (ObjectUtil.isNull(chinaCity)){
            if (retryTimes<5){
                chinaCity = getCityInfo(cityName, getBeforeDays(date, -1),retryTimes+1);
            }else {
                log.warn(cityName + "=》 疫情数据查询异常");
                ChinaCity city = new ChinaCity();
                city.setMark("error");
                return city;
            }
        }
        return chinaCity;
    }

    private RowHandler createRowHandler() {
        return new RowHandler() {
            @Override
            public void handle(int sheetIndex, long rowIndex, List<Object> rowList) {
                Console.log("[{}] [{}] {}", sheetIndex, rowIndex, rowList);
            }
//            @Override
//            public void handle(int sheetIndex, int rowIndex, List<Object> rowlist) {
//                Console.log("[{}] [{}] {}", sheetIndex, rowIndex, rowlist);
//            }
        };
    }


}
